package com.ybq.library.controller;

/**
 * @author : Ybq777
 * @version : 1.0.0
 * @description :
 * @createTime : 2024/12/2 16:55
 */

import com.ybq.library.entity.YbqBook;
import com.ybq.library.entity.YbqUser;
import com.ybq.library.service.YbqBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/books")
public class YbqBookController {

    @Autowired
    private YbqBookService bookService;

    // 新增图书信息接口
    //http://localhost:8080/books
    /*
    *{
    "YbqBookName" : "剑来",
    "YbqBookType" : "小说" ,
    "YbqBookPrice" : 100.0,
    "YbqBookCoverImage" : "aaa"
}
    * */
    @CrossOrigin(origins = "*", allowedHeaders = "*")
    @PostMapping
    public void addBook(@RequestBody YbqBook book) {
        bookService.addBook(book);
    }

    // 删除图书信息接口，需验证权限
    //http://localhost:8080/books/1
    @DeleteMapping("/{bookId}")
    @CrossOrigin(origins = "*", allowedHeaders = "*")
    public void deleteBookById(@PathVariable("bookId") String bookId) {
        bookService.deleteBookById(bookId);
    }

    // 修改图书信息接口，需验证权限
    //http://localhost:8080/books
    /*
    {
    "YbqId" : 2,
    "YbqBookName" : "剑来",
    "YbqBookType" : "小说" ,
    "YbqBookPrice" : 120.0,
    "YbqBookCoverImage" : "aaa"
}
    * */
    @PutMapping
    @CrossOrigin(origins = "*", allowedHeaders = "*")
    public void updateBook(@RequestBody YbqBook book) {
        bookService.updateBook(book);
    }

    // 按单条件查询图书信息接口
    //http://localhost:8080/books/single-condition?condition=YbqBookId&value=2
    @GetMapping("/single-condition")
    @CrossOrigin(origins = "*", allowedHeaders = "*")
    public YbqBook getBookBySingleCondition(@RequestParam("condition") String condition,
                                         @RequestParam("value") Object value) {
        return bookService.getBookBySingleCondition(condition, value);
    }

    // 按多条件查询图书信息接口
    @GetMapping("/multiple-conditions")
    @CrossOrigin(origins = "*", allowedHeaders = "*")
    public List<YbqBook> getBooksByMultipleConditions(@RequestParam("bookName") String bookName,
                                                   @RequestParam("bookType") String bookType,
                                                   @RequestParam("minPrice") double minPrice,
                                                   @RequestParam("maxPrice") double maxPrice) {
        return bookService.getBooksByMultipleConditions(bookName, bookType, minPrice, maxPrice);
    }
    // 查询所有用户信息接口
    //http://localhost:8080/users/all-books
    @GetMapping("/all-books")
    @CrossOrigin(origins = "*", allowedHeaders = "*")
    public List<YbqBook> getAllBooks() {
        return bookService.getAllBooks();
    }
    // http://localhost:8080/books/upload?bookId=1
    @PostMapping("/upload")
    @CrossOrigin(origins = "*", allowedHeaders = "*")
    public String uploadBookCover(@RequestParam("bookId") String bookId, @RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return "请选择要上传的文件";
        }
        // 定义文件存储的路径，这里简单示例存储到项目下的一个covers目录下，实际要根据需求调整，比如配置外部存储路径等
        String filePath = System.getProperty("user.dir") + "/covers/";
        File dest = new File(filePath);
        if (!dest.exists()) {
            dest.mkdirs();
        }
        // 生成新的文件名，这里使用UUID避免文件名冲突，也可以结合原文件名等信息来生成更合适的文件名
        String fileName = UUID.randomUUID().toString() + "-" + file.getOriginalFilename();
        try {
            file.transferTo(new File(dest, fileName));
            // 这里假设将文件存储路径更新到对应的图书记录中，调用服务层方法更新图书封面路径（假设已有对应的更新图书信息方法，这里简化示例）
            YbqBook book = new YbqBook();
            book.setYbqBookId(Long.parseLong(bookId));
            book.setYbqBookCoverImage(filePath + fileName);
            bookService.updateBook(book);
            return "封面上传成功";
        } catch (IOException e) {
            e.printStackTrace();
            return "封面上传失败";
        }
    }
}